<?php

// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2018 http://www.hisiphp.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>，开发者QQ群：50304283
// +----------------------------------------------------------------------

namespace plugins\sms;

use think\Cache;
use Mrgoon\AliSms\AliSms;
use app\common\controller\Plugins;

defined('IN_SYSTEM') or die('Access Denied');
/**
 * 短信插件.
 */
class sms extends Plugins
{
    /**
     * @var array 插件钩子清单
     */
    public $hooks = [
        // 钩子名称 => 钩子说明【系统钩子，说明不用填写】
        'send_sms',
    ];

    public function sendSms($params)
    {
        $redis = Cache::getHandler();

        $config = [];
        $send_sms_config = $this->getConfig('send_sms');

        foreach ($send_sms_config as $key => $value) {
            $config[$value['name']] = $value['value'];
        }
        $is_open = $config['is_open'];
        array_pop($config);
        if (empty($is_open)) {
            return;
        }
        $aliSms = new AliSms();
        $response = $aliSms->sendSms($params['tel'], $params['templatecode'], $params['data'], $config);
        $redis->zAdd('sendmessagehistory', date('YmdHis', time()), $params['tel']);
        if ($response->Code != 'OK') {
            $arr = [];
            $arr['code'] = 1;
            $arr['msg'] = $response->Message;
            $arr['data'] = $response->Code;
            echo \json_encode($arr);
            die;
        }
    }

    /**
     * 安装前的业务处理，可在此方法实现，默认返回true.
     *
     * @return bool
     */
    public function install()
    {
        return true;
    }

    /**
     * 安装后的业务处理，可在此方法实现，默认返回true.
     *
     * @return bool
     */
    public function installAfter()
    {
        return true;
    }

    /**
     * 卸载前的业务处理，可在此方法实现，默认返回true.
     *
     * @return bool
     */
    public function uninstall()
    {
        return true;
    }

    /**
     * 卸载后的业务处理，可在此方法实现，默认返回true.
     *
     * @return bool
     */
    public function uninstallAfter()
    {
        return true;
    }
}
